package com.kgc.crowd.distributioncrowddatabaseprovider.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.crowd.distributioncrowdcommon.pojo.*;
import com.kgc.crowd.distributioncrowdcommon.vo.ProjectVO;
import com.kgc.crowd.distributioncrowddatabaseprovider.service.OrderService;
import com.kgc.crowd.distributioncrowddatabaseprovider.service.ProjectService;
import com.kgc.crowd.distributioncrowddatabaseprovider.service.TypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.awt.print.Book;
import java.util.List;

@RestController
@RequestMapping("/database/provider")
public class ProjectController {
    @Resource
    ProjectService projectService;

    @Resource
    TypeService typeService;

    @Resource
    OrderService orderService;

    @RequestMapping("/save/project/remote/{memberId}")
    public ResultEntity<String> saveProjectRemote(
            @RequestBody ProjectVO projectVO,
            @PathVariable("memberId") String memberId) {

        try {
            projectService.saveProject(projectVO, memberId);

            return ResultEntity.successNoData();
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }

    }


    @RequestMapping("/select/all/type")
    public ResultEntity<List<TypePO>> selectTypePO() {

        try {
            List<TypePO> typePOList = projectService.selectTypePO();
            return ResultEntity.successWithData(typePOList);
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }

    }


    @RequestMapping("/select/all/tag")
    public ResultEntity<List<TagPO>> selectTagPO() {
        try {
            List<TagPO> tagPOList = projectService.selectTagPO();
            return ResultEntity.successWithData(tagPOList);
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }

    }

    @RequestMapping("/selectProjectByType")
    public ResultEntity<PageInfo<ProjectPO>> selectProjectByType(
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
            @RequestParam(value="typeId", defaultValue="0",required = false) Integer typeId,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ) {
        PageInfo<ProjectPO> pageInfo = projectService.selectProjectByType(pageNum, pageSize,typeId,keyword);
        return ResultEntity.successWithData(pageInfo);
    }

    @RequestMapping("/save/project")
    public ResultEntity<String> insertProject(@RequestBody ProjectPO projectPO) {
        try {
            projectService.insertProject(projectPO);
            return ResultEntity.successNoData();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

    @RequestMapping("/selectAllType")
    public ResultEntity<List<TypePO>> selectAllType() {
        List<TypePO> typePOList = typeService.selectAllType();
        return ResultEntity.successWithData(typePOList);
    }


    @RequestMapping("/select/project/itempic")
    public ResultEntity<ProjectPO> selectPorjectItemPid(@RequestParam(value="id", defaultValue="0",required = false) Integer id){
        ProjectPO projectPO = projectService.selectProjectAndItemPic(id);
        return ResultEntity.successWithData(projectPO);
    }
    @RequestMapping("/select/project/member")
    public ResultEntity<List<ProjectPO>> selectProjectByMemberId(@RequestParam(value="id", defaultValue="0",required = false) Integer id){
        List<ProjectPO> projectPOList = projectService.selectProjectByMemberId(id);
        return ResultEntity.successWithData(projectPOList);
    }

    @RequestMapping("/select/project/advertisement")
    public ResultEntity<List<ProjectPO>> selectProjectAndAdvertisement(@RequestParam(value="id", defaultValue="0",required = false) Integer id){
        List<ProjectPO> projectPOList = projectService.selectProjectAndAdvertisement(id);
        return ResultEntity.successWithData(projectPOList);
    }

    @RequestMapping("/select/project/order")
    public ResultEntity<List<Order>> selectOrderAndProject(@RequestParam(value="id", defaultValue="0",required = false) Integer id){
        List<Order> orders = orderService.selectOrderAndProject(id);
        return ResultEntity.successWithData(orders);
    }

    @RequestMapping("/select/project/return")
    public ResultEntity<ProjectPO> selectProjectAndReturn(@RequestParam(value="id", defaultValue="0",required = false) Integer id){
        ProjectPO projectPO = projectService.selectProjectAndReturn(id);
        return ResultEntity.successWithData(projectPO);
    }
}
